package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssBucketOneEnum;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.knowledge.domain.FileTemplateTree;
import cn.cdeden.knowledge.domain.QaFilesTree;
import cn.cdeden.knowledge.domain.TreeDownloadFiles;
import cn.cdeden.knowledge.domain.dto.QaFilesTreeDto;
import cn.cdeden.knowledge.enums.FileSuffixTypeEnum;
import cn.cdeden.knowledge.service.QaFilesTreeService;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.domain.*;
import cn.cdeden.system.api.RemoteUserService;
import cn.cdeden.system.api.model.LoginUser;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * AI问答附件上传实现
 */

@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "qa" ,version = "1.0.0")
public class OssUploadQaServiceImpl implements OssUploadService {

    private final QaFilesTreeService qaFilesTreeService;

    @DubboReference(check = false)
    private RemoteUserService remoteUserService;

    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        QaFilesTreeDto qaFilesTreeDto = qaFilesTreeService.findById(Long.valueOf(moduleId));

        RemoteFile remoteFile = new RemoteFile();
        if (StringUtils.isNotBlank(qaFilesTreeDto.getStorageId())) {
            remoteFile.setOssId(Long.valueOf(qaFilesTreeDto.getStorageId()));
        }
        remoteFile.setVersionId(qaFilesTreeDto.getEdition());
        remoteFile.setVersionName(qaFilesTreeDto.getEditionName());
        remoteFile.setFileSize(qaFilesTreeDto.getSize());
        remoteFile.setFileSuffix(qaFilesTreeDto.getSuffix());
        remoteFile.setModuleType(OssModuleType.Qa.getCode());
        remoteFile.setName(qaFilesTreeDto.getRealName());
        remoteFile.setOriginalName(qaFilesTreeDto.getRealName());

        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId) {
        info.setBrandName(OssBucketOneEnum.QA.getCode());
        return info;
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        LoginUser user = remoteUserService.getUserInfo(userId);
        QaFilesTree qaFilesTree = new QaFilesTree();
//        QaFilesTree qaFilesTree1 = qaFilesTreeService.selectByParentId(upload.getParentId());
        qaFilesTree.setParentId(upload.getParentId());

//        if (qaFilesTree1.getType().equals("head")) {
//            qaFilesTree.setSuperiorsIds(qaFilesTree1.getTopParentId());
//        } else {
//            qaFilesTree.setSuperiorsIds(qaFilesTree1.getSuperiorsIds() + "," +upload.getParentId());
//        }

        qaFilesTree.setRealName(upload.getFileName());

        qaFilesTree.setSuffix(upload.getFileSuffix());
//        qaFilesTree.setSuperiorsPath(qaFilesTree1.getPath());
        qaFilesTree.setType("file");
        qaFilesTree.setStorageId(upload.getStorageId());
        qaFilesTree.setStorageType("minio");
        qaFilesTree.setSize(upload.getFileSize());
        qaFilesTree.setEditionName(upload.getVersionName());
        qaFilesTree.setEdition(upload.getVersionId());
        qaFilesTree.setCreateId(userId.toString());
        qaFilesTree.setCreateBy(user.getUsername());
        qaFilesTree.setCreateTime(new Date());
        qaFilesTree.setUpdateId(userId.toString());
        qaFilesTree.setUpdateBy(user.getUsername());
        qaFilesTree.setUpdateTime(new Date());
        qaFilesTree.setRemarks(upload.getRemarks());
//        qaFilesTree.setPath(qaFilesTree1.getPath() + "/" + upload.getFileName());
        qaFilesTreeService.insertOne(qaFilesTree);

        return RemoteFilesBucketPath.of(OssBucketOneEnum.QA.getCode()).setUserList(Arrays.asList(user.getUserId().toString()));
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        List<TreeDownloadFiles> treeDownloadFiles = qaFilesTreeService.folderSonFiles(idList, true);
        return null;
    }
}
